//
//  WHBKPieChartView.m
//  RoolBK
//
//  Created by paykee on 16/6/30.
//  Copyright © 2016年 jpy. All rights reserved.
//

#import "WHBKPieChartView.h"

@interface WHBKPieChartView ()

@property(nonatomic,assign) float  workNum;//总数

@end

@implementation WHBKPieChartView

-(void)setDataResource:(NSArray *)dataResource{
    _dataResource = dataResource;
    
    for(int i = 0 ; i< _dataResource.count ; i++){
        
        self.workNum = [_dataResource[i] floatValue] + self.workNum;
        DLog(@"%lf",self.workNum);
    }
    

}
-(instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = self.frame.size.width / 2;
        self.layer.masksToBounds = YES;
        
        self.titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.frame.size.width * 0.28 , 40, self.frame.size.width * 0.5, self.frame.size.height * 0.2)];
        self.titleLabel.text = @"核心客户分析图";
        self.titleLabel.numberOfLines = 2;
        self.titleLabel.font = [UIFont systemFontOfSize:13];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.textColor = BHColor(51, 51, 51);
        [self addSubview:self.titleLabel];
        
        UILabel *subTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.frame.size.width * 0.25, CGRectGetMaxY(self.titleLabel.frame), 70, 22)];
        subTitleLabel.text = @"工单总数";
        subTitleLabel.font = [UIFont systemFontOfSize:16];
        subTitleLabel.textColor = [UIColor lightGrayColor];
        subTitleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:subTitleLabel];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(workNum:) name:workNumNoticeCenter object:nil];
        
    }
    return self;
}
- (void)workNum:(NSNotification *)noti{

    DLog(@"%@",noti.userInfo[@"key"]);
    self.workNum = [[NSString stringWithFormat:@"%@",noti.userInfo[@"key"]]floatValue];

}
- (void)dealloc{

    [[NSNotificationCenter defaultCenter]removeObserver:self];

}
-(void)drawRect:(CGRect)rect{

    CGContextRef ctr = UIGraphicsGetCurrentContext();
    int sum = 0;
    for (int  i = 0 ; i < self.dataResource.count; i++) {
        CGFloat startRadius = 0;
        CGFloat endRadius = 0;
        startRadius = (sum / self.workNum) *  M_PI * 2;
        if(i == 3){
            endRadius = 2 * M_PI;
        }else{
            endRadius = ([self.dataResource[i] floatValue] / self.workNum) *  M_PI * 2 + startRadius;
        }
        
        CGContextMoveToPoint(ctr, rect.size.width / 2, rect.size.height / 2);
        CGContextAddArc(ctr, rect.size.width / 2, rect.size.height / 2, self.frame.size.width / 2, startRadius, endRadius, 0);
        [[self colorWithIndex:i]set];
        CGContextFillPath(ctr);
        sum += [self.dataResource[i] floatValue];
    }

    CGContextMoveToPoint(ctr, rect.size.width / 2, rect.size.height / 2);
    CGContextAddArc(ctr, rect.size.width / 2, rect.size.height / 2, self.frame.size.width / 3.0, 0, 2 * M_PI, 0);
    [[UIColor whiteColor]set];
    CGContextFillPath(ctr);
}

-(UIColor *)colorWithIndex:(int)num{
    UIColor *color;
    switch (num) {
        case 0:
            color = BHColor(198, 198, 198);
            break;
        case 1:
            color = BHColor(215, 152, 3);
            break;
        case 2:
            color = Theme_Color;
            break;
        case 3:
            color = [UIColor colorWithRed:64.0/255.0 green:181.0/255.0 blue:211.0/255.0 alpha:1.0];
            break;
        default:
            break;
    }
    return color;
}


@end
